package com.tbit.api;

import com.alibaba.fastjson.JSON;
import com.tbit.common.constant.CommonConstant;
import com.tbit.manager.api.NoticeApi;
import com.tbit.manager.api.dto.NoticeDTO;
import com.tbit.manager.api.dto.NoticeSearchDTO;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.Notice;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.business.AccountUserService;
import com.tbit.uqbike.service.business.NoticeService;
import com.tbit.uqbike.webmanager.dao.core.RoleDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class NoticeApiImpl implements NoticeApi {

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private RoleDao roleDao;


    /**
     * @description: 获取用户的公告集合
     * @author: mo.shanyong
     * @date: 2025/2/21 15:10
     * @param: noticeSearchDTOStr 搜索条件实体转变的字符串
     * @return: java.util.List<com.tbit.manager.api.dto.NoticeDTO>
     */
    @Override
    public Map<String, Object> getUserNoticeList(String noticeSearchDTOStr) {
        NoticeSearchDTO searchDTO = JSON.parseObject(noticeSearchDTOStr, NoticeSearchDTO.class);
        Map<String, Object> map = new HashMap<>();
        LoginInfo loginInfo = this.getLoginInfo(searchDTO.getUserId(), searchDTO.getBrandId(),
                searchDTO.getAccountUserType(),searchDTO.getAccountId());
//        log.info("getUserNoticeList---------------->loginInfo:{}",JSON.toJSONString(loginInfo));
        List<Notice> userNoticeList = noticeService.getUserNoticeList(searchDTO.getPageNum(), searchDTO.getPageSize(), loginInfo, CommonConstant.NOTICE_MAIN_PLATFORM);
        int total = noticeService.getUserNoticeCount(loginInfo,CommonConstant.NOTICE_MAIN_PLATFORM);
        int userNotReadNoticeCount = noticeService.getUserNotReadNoticeCount(CommonConstant.NOTICE_MAIN_PLATFORM, loginInfo);
        map.put("total",total);
        map.put("data",userNoticeList);
        map.put("num",userNotReadNoticeCount);
        return map;
    }
    /**
     * @description: 记录公告日志
     * @author: mo.shanyong
     * @date: 2025/2/21 15:22
     * @param: noticeDTO
     * @return: java.lang.Integer
     */
    @Override
    public Integer recordNoticeLog(NoticeDTO noticeDTO) {
        LoginInfo info = new LoginInfo();
        info.setAccountUserId(noticeDTO.getUserId());
        return noticeService.recordNoticeLog(noticeDTO.getNoticeId(),noticeDTO.getOperateType(),info,CommonConstant.NOTICE_MAIN_PLATFORM);
    }

    /**
     * @description: 获取登录用户
     * @author: mo.shanyong
     * @date: 2025/2/26 17:05
     * @param: userId 用户id
     * @param: brandId 品牌id
     * @param: accountUserType 用户类型
     * @param: accountId 区域id
     * @return: com.tbit.uqbike.object.value.LoginInfo
     */
    private LoginInfo getLoginInfo(Integer userId,Integer brandId,Integer accountUserType,Integer accountId){
        AccountUser accountUser = accountUserService.getAccountUserById(userId);
        return new LoginInfo(accountId,accountUserType, accountUser.getAccountUserId(),
                accountUser.getPhone(), accountUser.getName(), roleDao.selectRolePermissionByUserId(accountUser.getAccountUserId()), brandId);
    }
}
